package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xx.job.entity.system.SysMenu;
import com.xx.job.entity.system.SysRole;
import com.xx.job.entity.system.SysRoleMenu;
import com.xx.job.mapper.SysRoleMenuMapper;
import com.xx.job.service.system.SysMenuService;
import com.xx.job.service.system.SysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    
    @Autowired
    private SysMenuService sysMenuService;



    @Override
    public int add(SysRoleMenu sysRoleMenu) {
        return 0;
    }

    @Override
    public int add(SysRole sysRole) {
        List<Long> menuIds = sysRole.getMenuIds();
        Long roleId = sysRole.getRoleId();
        String roleName = sysRole.getRoleName();
        menuIds.forEach(x ->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(x);
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setRoleName(roleName);
            sysRoleMenuMapper.insert(sysRoleMenu);
        });
        return 0;
    }

    @Override
    public int delete(Long id) {
        return 0;
    }

    @Override
    public int delete(List<Long> ids) {
        return sysRoleMenuMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByMap(Long roleId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("role_id",roleId);
        return sysRoleMenuMapper.deleteByMap(columnMap);
    }

    @Override
    public int update(SysRoleMenu sysRoleMenu) {
        return 0;
    }

    @Override
    public SysRoleMenu select(long id) {
        return null;
    }

    @Override
    public List<SysRoleMenu> selectList() {
        return null;
    }

    @Override
    public List<Long> selectByRoleId(long roleId) {
        List<Long> menuIds = new ArrayList<>();
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("role_id",roleId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectByMap(columnMap);
        sysRoleMenus.forEach(x->{
            menuIds.add(x.getMenuId());
        });
        return menuIds;
    }

    /**
     * 角色管理选中框，
     * @param roleId
     * @return
     */
    @Override
    public List<Long> selectCheckedKeys(long roleId) {
        List<Long> menuIds = new ArrayList<>();
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<String> types = new ArrayList<>();
        types.add("A");
        types.add("F");

        List<Long> ids = sysMenuService.selectByType(types);
        wrapper.in("menu_id",ids);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(wrapper);
        sysRoleMenus.forEach(x->{
            menuIds.add(x.getMenuId());
        });
        return menuIds;
    }

    
    public Map<String,Object> roleMenuTreeselect(long roleId){
        Map<String,Object> data = new HashMap<>();
        List<Long> menuIds = this.selectByRoleId(roleId);
        List<Map<String, Object>> menuTree = sysMenuService.menuTreeselect();
        data.put("checkedKeys",menuIds);
        data.put("menus",menuTree);
        data.put("code",200);
        return data;
    }
}
